package com.zhiyizhen.callnumber.common;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * description 日期格式转换
 *
 * @author dahu
 */
public class DateUtil {

    private DateUtil() {
    }

    public static final String YYYY_MM = "yyyy-MM";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_ZH = "yyyy年MM月";
    public static final String YYYY_MM_DD_ZH = "yyyy年MM月dd日";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String MM_DD_HH_MM = "MM-dd HH:mm";
    public static final String MM_DD_HH_MM1 = "MM月dd日 HH:mm";
    public static final String MM_DD_HH_MM2 = "MM月dd日HH:mm";
    public static final String MM_DD = "MM月dd日";
    public static final String HH_MM = "HH:mm";

    public static final long MINUTE = 60 * 1000L;
    public static final long HOUR = 60 * MINUTE;
    public static final long ONE_DAY_TIMESTAMP = 24 * HOUR;

    public static final int FULL_TIME_LENGTH = 19;

    /**
     * 获取对应日期字符串的 unix 时间戳
     * @return 处理成功返回对应的 unix 时间戳，处理失败返回
     */
    public static long getStringToDate(String dateString, String dateFormat) {
        if (dateString == null || dateFormat == null) {
            return Long.MIN_VALUE;
        }

        Date date = null;
        try {
            date = getDateFormat(dateFormat).parse(dateString);
        } catch (ParseException e) {
            LogUtil.e(e);
        }
        if (date != null) {
            return date.getTime();
        }
        return Long.MIN_VALUE;
    }

    /**
     * 将日期格式进行转换
     * @return 转换成功输出对应格式，转换失败返回 null
     */
    public static String getDateString(String dateString, String originFormat, String targetFormat) {
        if (dateString == null || originFormat == null || targetFormat == null) {
            return null;
        }

        Date date = null;
        try {
            date = getDateFormat(originFormat).parse(dateString);
        } catch (ParseException e) {
            LogUtil.e(e);
        }
        if (date != null) {
            return getDateFormat(targetFormat).format(date);
        }
        return null;
    }

    /**
     * 转换日期时间，去掉年和秒
     *
     * @param fullDateTime yyyy-MM-dd HH:mm:ss
     * @return MM月dd日 HH:mm
     */
    public static String convertDateTime(String fullDateTime) {
        if (fullDateTime == null) {
            return null;
        }
        final String regex = "(\\d{4})-(\\d{1,2})-(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})";
        final String replacement = "$2月$3日 $4:$5";
        return fullDateTime.replaceAll(regex, replacement);
    }

    /**
     * 切割日期日间格式，返回日期
     *
     * @param dateTime 完整时间格式：2020-10-20 12:30:22
     * @return 2020-10-20
     */
    public static String getDateFromDateTime(String dateTime) {
        if (!TextUtils.isEmpty(dateTime)) {
            String[] all = dateTime.split("\\s+");
            if (all.length > 1) {
                return all[0];
            }
        }
        return "";
    }

    /**
     * 切割日期日间格式，返回时间
     *
     * @param dateTime 完整时间格式：2020-10-20 12:30:22
     */
    public static String getTimeFromDateTime(String dateTime) {
        if (!TextUtils.isEmpty(dateTime)) {
            String[] all = dateTime.split("\\s+");
            if (all.length > 1) {
                return all[1];
            }
        }
        return "";
    }

    /**
     * 获取当前时间
     */
    public static String getCurrentTime() {
        Date date = new Date(System.currentTimeMillis());
        return getDateFormat(HH_MM).format(date);
    }

    public static String getCurrentDate() {
        Date date = new Date(System.currentTimeMillis());
        return getDateFormat(YYYY_MM_DD_ZH).format(date);
    }

    /**
     * 某日是否今天
     *
     * @param time 满足"yyyy-MM-dd"的格式
     */
    public static boolean isToday(String time) {
        Date date = new Date(System.currentTimeMillis());
        String currentDate = getDateFormat(YYYY_MM_DD).format(date);
        return time.trim().equals(currentDate.trim());
    }

    public static boolean isToday(String timeStr, String dateFormat) {
        long todayStartTime = getTodayStartTime();
        long time = DateUtil.getStringToDate(timeStr, dateFormat);
        return time >= todayStartTime && time < todayStartTime + ONE_DAY_TIMESTAMP;
    }

    public static boolean isCurrentMonth(String otherDate) {
        if (!TextUtils.isEmpty(otherDate)) {
            Date date = new Date(System.currentTimeMillis());
            String currentDate = getDateFormat(YYYY_MM_DD).format(date);
            return otherDate.trim().startsWith(currentDate);
        }
        return false;
    }

    /**
     * 获取当天0点的时间戳
     */
    public static long getTodayStartTime() {
        long nowTime = System.currentTimeMillis();
        return nowTime - ((nowTime + TimeZone.getDefault().getRawOffset()) % ONE_DAY_TIMESTAMP);
    }
    /**
     * 判断输入的日期是否今天以后
     * @param dateStr 2020-11-11，格式固定
     */
    public static boolean isAfterToday(String dateStr) {
        long date = DateUtil.getStringToDate(dateStr, YYYY_MM_DD);
        return date - getTodayStartTime() > 0;
    }

    public static SimpleDateFormat getDateFormat(String format) {
        return new SimpleDateFormat(format, Locale.CHINA);
    }

    // 获取当前星期几的文字描述
    public static String getCurrentWeekday() {
        LocalDate today = LocalDate.now();
        DayOfWeek dayOfWeek = today.getDayOfWeek();

        // 直接返回中文星期几
        return switch (dayOfWeek) {
            case MONDAY -> "星期一";
            case TUESDAY -> "星期二";
            case WEDNESDAY -> "星期三";
            case THURSDAY -> "星期四";
            case FRIDAY -> "星期五";
            case SATURDAY -> "星期六";
            case SUNDAY -> "星期日";
        };
    }
}
